package com.example.skillcinema.ui.screens.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.skillcinema.adapters.FilmRecyclerViewAdapter
import com.example.skillcinema.databinding.FragmentHomeBinding
import com.example.skillcinema.db.displaymodels.FilmDisplayModel
import com.example.skillcinema.db.enums.FilmFetchType
import com.example.skillcinema.db.responses.FiltersCountriesResponse
import com.example.skillcinema.db.responses.FiltersGenresResponse
import com.example.skillcinema.ui.screens.main.viewmodels.MainViewModel
import kotlinx.coroutines.launch


class HomeFragment : Fragment() {
    private var _binding: FragmentHomeBinding? = null
    private val binding: FragmentHomeBinding
        get() = _binding!!

    private val viewModel: MainViewModel by activityViewModels<MainViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var filters: Pair<FiltersCountriesResponse, FiltersGenresResponse>
        lifecycleScope.run {
            launch {
                viewModel.premierFlow.collect {
                    binding.premiersFilmCollection.setTitle("Премьеры")
                    binding.premiersFilmCollection.setData(it)
                    binding.premiersFilmCollection.setFilmFetchType(FilmFetchType.PREMIERS)
                }
            }
            launch {
                viewModel.topAllFlow.collect {
                    binding.topFilmCollection.setTitle("Популярное")
                    binding.topFilmCollection.setData(it)
                    binding.topFilmCollection.setFilmFetchType(FilmFetchType.TOP_ALL)
                }
            }
            launch {
                filters =
                    viewModel.filmFilters.value
                binding.randomFilmCollection.setTitle("${filters.first.country} ${filters.second.genre}")
                binding.randomFilmCollection.setCollectionFilters(
                    Pair<Int, Int>(filters.first.id, filters.second.id)
                )
                binding.randomFilmCollection.setFilmFetchType(FilmFetchType.MIX_FILTERED)
            }
            launch {
                viewModel.filmFiltered.collect {
                    binding.randomFilmCollection.setData(it)
                }
            }
            launch {
                viewModel.top250Flow.collect {
                    binding.top250FilmCollection.setTitle("Топ-250")
                    binding.top250FilmCollection.setData(it)
                    binding.top250FilmCollection.setFilmFetchType(FilmFetchType.TOP_250)
                }
            }
            launch {
                viewModel.serialFlow.collect {
                    binding.serialsFilmCollection.setTitle("Сериалы")
                    binding.serialsFilmCollection.setData(it)
                    binding.serialsFilmCollection.setFilmFetchType(FilmFetchType.SERIALS)
                }
            }
        }
    }
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}